var app = getApp();
var wUtils = require('../../utils/wechatRequest.js');
import { $wuxDialog } from '../../dist/index';
Page({

   /**
    * 页面的初始数据
    */
   data: {
      fun: [{
            key: 0,
            title: '关注',
            num: 10,
            url: '/pages/follow/follow'
         },
         {
            key: 1,
            title: '粉丝',
            num: 99,
            url: '/pages/fans/fans'
         },
         {
            key: 2,
            title: '积分',
            num: 1203,
            url: '/pages/integralHome/integralHome'
         },

      ],
     canIUse: wx.canIUse('button.open-type.getUserInfo'),
     hasUserInfo: false,
     userInfo:{}
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
     const scene = decodeURIComponent(options.scene);
     console.log(scene)
     let inviteUserId;
     let qrCodeId;
     if (typeof (exp) == "undefined" ) {
       inviteUserId = "";
       qrCodeId = "";
     } else {
       let params = scene.split(",");
       inviteUserId = params[0];
       qrCodeId = params[1];
     }
     var that = this;
     app.getToken(function (data) {
       app.globalData.token = data;
       wUtils.doGet('/app/user/info', null, function (res) {
         if ( res.code == 0 ) {
           app.globalData.userInfo = res.userEntity;
           if (res.userEntity.headimgurl == null || res.userEntity.nickname == null) {
             // 弹出显示授权按钮
            //   wx.getUserInfo({
            //      success: function (res) {
            //         console.log(res)
            //      }
            //   })
            console.log('未授权！')
            wx.redirectTo({
               url: '/pages/authorize/authorize',
            })
           } else {
             that.setData({
               userInfo: res.userEntity,
               hasUserInfo: true
             })
           }
         }
       });
     }, inviteUserId, qrCodeId);
	 
	//  toSearch() {
  //     wx.navigateTo({
  //        url: '/pages/search/search',
  //     })

   },
   	 toSearch() {
      wx.navigateTo({
         url: '/pages/search/search',
      })},
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo;
    app.globalData.userInfo.sex = e.detail.userInfo.gender;
    app.globalData.userInfo.nickname = e.detail.userInfo.nickName;
    app.globalData.userInfo.headimgurl = e.detail.userInfo.avatarUrl;
    wUtils.doPost('/app/wechat/save/userinfo', app.globalData.userInfo, function (res) {
      if(res.code == 0) {
        console.log('授权成功.');
      }
    });
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
   toFun(e) {
      var obj = e.currentTarget.dataset;
      wx.navigateTo({
         url: obj.url,
      })
   },
   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {
     
   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function() {

   }
})